# import time
# from threading import Thread
# from threading import current_thread
#
# flag = False
#
#
# def task():
#     print(f'{current_thread().name} 检测服务器是否正常开启....')
#     time.sleep(3)
#     global flag
#     flag = True
#
#
# def task1():
#     while 1:
#         time.sleep(1)
#         print(f'{current_thread().name} 正在尝试连接服务器.....')
#         if flag:
#             print('连接成功')
#             return
#
# if __name__ == '__main__':
#     t1 = Thread(target=task1,)
#     t2 = Thread(target=task1,)
#     t3 = Thread(target=task1,)
#
#     t = Thread(target=task)
#
#
#     t.start()
#     t1.start()
#     t2.start()
#     t3.start()



import time
from threading import Thread
from threading import current_thread
from threading import Event

event = Event()  # 默认是False
def task():
    print(f'{current_thread().name} 检测服务器是否正常开启....')
    time.sleep(3)
    event.set()  # 改成了True

def task1():
    print(f'{current_thread().name} 正在尝试连接服务器')
    # event.wait()  #　轮询检测event是否为Ｔrue,当其为True,继续下一行代码. 阻塞.
    event.wait(1)
    # 设置超时时间,如果1s中以内,event改成True,代码继续执行.
    # 设置超时时间,如果超过1s中,event没做改变,代码继续执行.
    print(f'{current_thread().name} 连接成功')
if __name__ == '__main__':
    t1 = Thread(target=task1,)
    t2 = Thread(target=task1,)
    t3 = Thread(target=task1,)

    t = Thread(target=task)


    t.start()
    t1.start()
    t2.start()
    t3.start()